/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.doc.sender.ui;

import com.database.entity.*;
import database.senderUtil;
import java.util.List;
import java.util.Vector;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author parent
 */
class Item
    {
        private int id;
        private String description;

        public Item(int id, String description)
        {
            this.id = id;
            this.description = description;
        }

        public int getId()
        {
            return id;
        }

        public String getDescription()
        {
            return description;
        }

        public String toString()
        {
            return description;
        }
    }
public class PopulateToList {
   Object context; 
   public PopulateToList() {
       
   }

    public PopulateToList(Object context) {
        this.context = context;
    }
   public DefaultComboBoxModel GetDocumentComboModel(){
       Session session = senderUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
             
            transaction = session.beginTransaction();
            String queryString = "from Documents order by document_no desc";
            Query query = session.createQuery(queryString);
            List listRecord=query.list();
            DefaultComboBoxModel model=new DefaultComboBoxModel();
            if (listRecord!=null){
                    for (Object o:listRecord){
                        Documents ds=(Documents)o;  
                        model.addElement(new Item(ds.getId(),ds.getLetterSubject()));
                    }
                  
                }
            transaction.commit();
            session.close();    
            return model;
        } catch (Exception e){
            //return false;
            return null;
        }
   } 
   public DefaultComboBoxModel GetFileComboModel(){
       Session session = senderUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
             
            transaction = session.beginTransaction();
            String queryString = "from PdfFiles order by file_name asc";
            Query query = session.createQuery(queryString);
            List listRecord=query.list();
            DefaultComboBoxModel model=new DefaultComboBoxModel();
            if (listRecord!=null){
                    for (Object o:listRecord){
                        PdfFiles ds=(PdfFiles)o;  
                        model.addElement(new Item(ds.getId(),ds.getFileName()));
                    }
                  
                }
            transaction.commit();
            session.close();    
            return model;
        } catch (Exception e){
            //return false;
            return null;
        }
   } 
   public DefaultComboBoxModel GetJobComboModel(){
       Session session = senderUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
             
            transaction = session.beginTransaction();
            String queryString = "from Jobs order by job_name asc";
            Query query = session.createQuery(queryString);
            List listRecord=query.list();
            DefaultComboBoxModel model=new DefaultComboBoxModel();
            if (listRecord!=null){
                    for (Object o:listRecord){
                        Jobs ds=(Jobs)o;  
                        model.addElement(new Item(ds.getId(),ds.getJobName()));
                    }
                  
                }
            transaction.commit();
            session.close();    
            return model;
        } catch (Exception e){
            //return false;
            return null;
        }
   }
   public DefaultComboBoxModel GetDocumentSourceComboModel(){
       Session session = senderUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
             
            transaction = session.beginTransaction();
            String queryString = "from DocumentSources order by source_name";
            Query query = session.createQuery(queryString);
            List listRecord=query.list();
            DefaultComboBoxModel model=new DefaultComboBoxModel();
            if (listRecord!=null){
                    for (Object o:listRecord){
                        DocumentSources ds=(DocumentSources)o;  
                        model.addElement(new Item(ds.getId(),ds.getSourceName()));
                    }
                  
                }
            transaction.commit();
            session.close();    
            return model;
        } catch (Exception e){
            //return false;
            return null;
        }
   } 
   public DefaultComboBoxModel GetGroupComboModel(){
       Session session = senderUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
             
            transaction = session.beginTransaction();
            String queryString = "from UserGroups order by group_name asc";
            Query query = session.createQuery(queryString);
            List listRecord=query.list();
            DefaultComboBoxModel model=new DefaultComboBoxModel();
            if (listRecord!=null){
                    for (Object o:listRecord){
                        UserGroups ds=(UserGroups)o;  
                        model.addElement(new Item(ds.getId(),ds.getGroupName()));
                    }
                  
                }
            transaction.commit();
            session.close();    
            return model;
        } catch (Exception e){
            //return false;
            return null;
        }
   }
   public DefaultComboBoxModel GetUserComboModel(){
       Session session = senderUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
             
            transaction = session.beginTransaction();
            String queryString = "from UserUsers order by user_name asc";
            Query query = session.createQuery(queryString);
            List listRecord=query.list();
            DefaultComboBoxModel model=new DefaultComboBoxModel();
            if (listRecord!=null){
                    for (Object o:listRecord){
                        UserUsers ds=(UserUsers)o;  
                        model.addElement(new Item(ds.getId(),ds.getUserName()));
                    }
                  
                }
            transaction.commit();
            session.close();    
            return model;
        } catch (Exception e){
            //return false;
            return null;
        }
   }
   public DefaultTableModel GetRecipientDocumentTableModel(int userid){
       Session session = senderUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
             
            transaction = session.beginTransaction();
            String queryString;
            if (userid!=0){
            queryString= "from Documents where next_recipient=:rec"
                    + "order by letter_subject asc";
            } else {
              queryString= "from Documents "
                    + "order by letter_subject asc";  
            }
            Query query = session.createQuery(queryString);
            if (userid!=0)query.setInteger("rec", userid);
            List listRecord=query.list();
            
//            DefaultTableModel model=new DefaultTableModel();
            if (listRecord!=null){
                    Vector<String> tableHeaders = new Vector<String>();
                    Vector tableData = new Vector();
                    tableHeaders.add("Id"); 
                    tableHeaders.add("LetterSubject");
                    tableHeaders.add("DocumentNo");
                    tableHeaders.add("DocumentDate");
                    tableHeaders.add("AgendaNo");
            //    
                for(Object o : listRecord) {
                    Documents ds = (Documents)o;
                    Vector<Object> oneRow = new Vector<Object>();
                    oneRow.add(ds.getId());
                    oneRow.add(ds.getLetterSubject());
                //oneRow.add("[BLOB "+Integer.toString(PdfFiles.getId())+"]");
                    oneRow.add(ds.getDocumentNo());
                    oneRow.add(ds.getDocumentDate());
                    oneRow.add(ds.getAgendaNo());
                    tableData.add(oneRow);
                }
                transaction.commit();
                session.close(); 
                DefaultTableModel  model=new DefaultTableModel(tableData, tableHeaders);
                return model;
            } else return null; 
            
           
               
            
        } catch (Exception e){
            //return false;
            return null;
        }
   }
   public DefaultTableModel GetJobTableModel(String keyword){
       Session session = senderUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
             
            transaction = session.beginTransaction();
            String queryString = "from Jobs "
                    + "order by job_name asc";
            Query query = session.createQuery(queryString);
//            query.setInteger("rec", userid);
            List listRecord=query.list();
            
//            DefaultTableModel model=new DefaultTableModel();
            if (listRecord!=null){
                    Vector<String> tableHeaders = new Vector<String>();
                    Vector tableData = new Vector();
                    tableHeaders.add("Id"); 
                    tableHeaders.add("JobName");
                    tableHeaders.add("JobDescription");
                    tableHeaders.add("Head");

            //    
                for(Object o : listRecord) {
                    Jobs ds = (Jobs)o;
                    Vector<Object> oneRow = new Vector<Object>();
                    oneRow.add(ds.getId());
                    oneRow.add(ds.getJobName());
                //oneRow.add("[BLOB "+Integer.toString(PdfFiles.getId())+"]");
                    oneRow.add(ds.getJobDescription());
                    UserUsers heads=(UserUsers)ds.getUserUserses();
                    oneRow.add(heads.getFullName());
                    
                    tableData.add(oneRow);
                }
                transaction.commit();
                session.close(); 
                DefaultTableModel  model=new DefaultTableModel(tableData, tableHeaders);
                return model;
            } else return null; 
            
           
               
            
        } catch (Exception e){
            //return false;
            return null;
        }
   }
   public DefaultTableModel GetGroupTableModel(String keyword){
       Session session = senderUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
             
            transaction = session.beginTransaction();
            String queryString;
            
            Query query;
            if (keyword.isEmpty()){
                queryString= "from UserGroups "
                    + "order by group_name asc";
            } else {
                queryString= "from UserGroups where group_desc like :k "
                    + "order by group_name asc";
            }
            query= session.createQuery(queryString);
            if (!keyword.isEmpty())query.setString("k","%"+ keyword+"%");
            List listRecord=query.list();
            
//            DefaultTableModel model=new DefaultTableModel();
            if (listRecord!=null){
                    Vector<String> tableHeaders = new Vector<String>();
                    Vector tableData = new Vector();
                    tableHeaders.add("Id"); 
                    tableHeaders.add("GroupName");
                    tableHeaders.add("Description");
//                    tableHeaders.add("Member Count");
            //    
                for(Object o : listRecord) {
                    UserGroups ds = (UserGroups)o;
                    Vector<Object> oneRow = new Vector<Object>();
                    oneRow.add(ds.getId());
                    oneRow.add(ds.getGroupName());
                //oneRow.add("[BLOB "+Integer.toString(PdfFiles.getId())+"]");
                    oneRow.add(ds.getGroupDesc());
//                    UserUsers members=(UserUsers)ds.getUserUserses();
                    
                   // oneRow.add(Integer.toString(members.()));
                    
                    tableData.add(oneRow);
                }
                DefaultTableModel  model=new DefaultTableModel(tableData, tableHeaders);
                transaction.commit();
                session.close(); 
                
                return model;
            } else return null; 
            
           
               
            
        } catch (Exception e){
          System.out.println(e.getMessage());
            return null;
        }
   }
   public DefaultTableModel GetUserTableModel(String keyword){
       Session session = senderUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try {
             
            transaction = session.beginTransaction();
            String queryString;
            
            Query query;
            if (keyword.isEmpty()){
                queryString= "from UserUsers "
                    + "order by user_name asc";
            } else {
                queryString= "from UserUsers where full_name like :k "
                    + "order by user_name asc";
            }
            query= session.createQuery(queryString);
            if (!keyword.isEmpty())query.setString("k","%"+ keyword+"%");
            List listRecord=query.list();
            
//            DefaultTableModel model=new DefaultTableModel();
            if (listRecord!=null){
                    Vector<String> tableHeaders = new Vector<String>();
                    Vector tableData = new Vector();
                    tableHeaders.add("Id"); 
                    tableHeaders.add("UserName");
                    tableHeaders.add("Fullname");
                    tableHeaders.add("Job Description");
            //    
                for(Object o : listRecord) {
                    UserUsers ds = (UserUsers)o;
                    Vector<Object> oneRow = new Vector<Object>();
                    oneRow.add(ds.getId());
                    oneRow.add(ds.getUserName());
                //oneRow.add("[BLOB "+Integer.toString(PdfFiles.getId())+"]");
                    oneRow.add(ds.getFullName());
                    Jobs job=(Jobs)ds.getJobs();
                    
                    oneRow.add(job.getJobName());
                    
                    tableData.add(oneRow);
                }
                DefaultTableModel  model=new DefaultTableModel(tableData, tableHeaders);
                transaction.commit();
                session.close(); 
                
                return model;
            } else {
                
                return null;
            } 
            
           
               
            
        } catch (Exception e){
          System.out.println(e.getMessage());
            return null;
        }
   }
   
}
